/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package graphic;

import java.io.File;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author maru
 */
public class StartGamePanel extends JPanel{
    private JTextField userT;
    private JButton ok;
    private StartFrame frame;
    private StartPanel panel;
    private Set<File> archLevels;
    
    public StartGamePanel(StartFrame frame, StartPanel panel){
        this.frame=frame;
        this.panel=panel;
        
        setLayout(null);
        JLabel userL = new JLabel("Ingrese Usuario:");
        userL.setBounds(50, 100, 150, 40);
        add(userL);
        userT = new JTextField("");
        userT.setBounds(220, 100, 100, 40);
        add(userT);

		File arch= new File(java.lang.System.getProperty("user.dir") + "/levels/");
		File aux[]=arch.listFiles();
		archLevels= new TreeSet<File>(new Comparator<File>(){
			public int compare(File f1, File f2) {
				return f1.getName().compareTo(f2.getName());
			}
		});
		for(File archivo: aux)
			if(archivo.getName().contains(".txt"))
				archLevels.add(archivo); 
		
        ok = new JButton("Aceptar");
        ok.setBounds(150, 300, 100, 40);
        add(ok);

        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                    okActionPerformed();
            }
        });
    }

    private void okActionPerformed(){
        String user = IngresaUser();
        if (!user.isEmpty()){
        	GamePanel gPanel = new GamePanel(frame, panel, archLevels, user, false);
            frame.add(gPanel);
            gPanel.setVisible(true);
            this.setVisible(false);
            remove(this);	
        }else{
        	JOptionPane.showMessageDialog(this,
			"El usuario no puede ser vacio",
			"Error en nombre de usuario",
			JOptionPane.WARNING_MESSAGE);
        }
    }

    private String IngresaUser(){
        return userT.getText().trim();
    }
}
